package com.example.fz338.ui.video.detail;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fz338.R;
import com.example.fz338.adapter.VideoListAdapter;

import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class VideoListFragment extends Fragment {
    private final VideoDetailFragment videoDetailFragment;
    private List<String> list;
    private String url0 = "http://vjs.zencdn.net/v/oceans.mp4";
    private String url1 = "http://vjs.zencdn.net/v/oceans.mp4";
    // url不行的话用这个 https://blog.csdn.net/weixin_41010198/article/details/88055078

    // 有的时候视频播不出来要多点几次播放键 ... 多等等 这网速...
    public VideoListFragment(String[] list, VideoDetailFragment videoDetailFragment) {
        this.list = Arrays.asList(list);
        this.videoDetailFragment = videoDetailFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_video_list, container, false);
        RecyclerView recyclerView = root.findViewById(R.id.recyclerView);
        VideoListAdapter videoListAdapter = new VideoListAdapter(list);
        recyclerView.setAdapter(videoListAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(root.getContext(),
                DividerItemDecoration.VERTICAL));
        videoListAdapter.setOnItemClickListener((adapter, view, position) -> {
            if(position % 2 == 0) {
                videoDetailFragment.playNewUrl(url0);
            } else {
                videoDetailFragment.playNewUrl(url1);
            }
        });
        return root;
    }
}
